
<?php
echo $form->create('User',array('action'=>'admin_search','class'=>"admin_search"));
echo $form->input('username');
if(cr('GeneralConfig.disable_editor_reviews') == false)
{
	echo $form->input('type',array('type'=>'select','empty'=>__('All'),'options'=>array('user'=>__('User'),'editor'=>__('Editor'))));
}
 echo $form->submit(__("Search"));?><div class="clear"></div></form><?php
?>
<div class="clear"></div>
<?php
	echo "<div class='switcher'>";
	echo __("Switch to ").$html->link("User Fields",array("plugin"=>null,"controller"=>"user_fields","action"=>"index"));
	echo "</div>";
	
	$Model = cri('Users.User');
	$fielPropertyObject = $Model->getAttributes();
	$customFields = array();
	$customFields = $fielPropertyObject->getFields('group','index');
	$showFields = array('id',array('created'=>'Date Joined'),'username','email');
	//merge custome fields.
	if($customFields)
	{
		$showFields = am($showFields,$customFields);
	}
	
	$index->setData($users, 'User');
	$index->sortableFields(array('id','username'));
	$index->showFields($showFields);
		
	$userIdVar = ':User.id';
	$index->setAction('Edit', array('action'=>'edit', $userIdVar));
	$index->setAction('View', array('action'=>'view', $userIdVar));
	$index->setAction('Delete', array('action'=>'delete', $userIdVar),$this->helpers,__('Warning! Deleting this user will delete all associated listings, reviews, comments and images. Are you sure you want to delete this user ?'));
	if($Model->emailVerification == true)
	{
		$index->setAction(__('Resend Verification'), array('action'=>'resendSignupNotification', $userIdVar), array(), false, true, 'filterEmailVerifyAction');
	}
	$index->setAction(null ,null ,$this->helpers,false,true,'toggleEditor');

	$index->addColumn('username','formateUsername');
	$index->addColumn('email','formateEmail');
	
	function formateUsername($record)
	{
		App::import('Helper','Html');
		$Html = new HtmlHelper;
		return $Html->link($record['User']['username'],'/admin/users/view/'.$record['User']['id']);
	}
	
	function formateEmail($record)
	{
		uses("Flay");
		return Flay::fragment($record['User']['email'],20);
	}
	
	function toggleEditor($model,$record)
	{
		if($record[$model]['is_editor'] == User::ACTIVATED_EDITOR)
		{
			$action['title'] = __('Degrade to user');
			$action['url'] = '/admin/users/toggleEditor/'.$record[$model]['id'];
		}else
		{
			$action['title'] = __('Upgrade to editor');
			$action['url'] = '/admin/users/toggleEditor/'.$record[$model]['id'];
		}
		return $action;
	}
	function filterEmailVerifyAction($userModelAlias, $record)
	{
		if($record[$userModelAlias]['emailVerified'] == 0)
		{
			return true; // keep the link same for unverified users
		}
		return false;
	}
	$index->setPaginateSeparator(' ');
	echo $index->display();
?>